package com.ecoolex.ying.space.api.graphql;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.UserFavorite;
import com.ecoolex.ying.space.entity.UserFavoriteItems;
import com.ecoolex.ying.space.pojo.AddUserFavoriteDTO;
import com.ecoolex.ying.space.pojo.EditUserFavoriteDTO;
import com.ecoolex.ying.space.ws.UserFavoriteItemsService;
import com.ecoolex.ying.space.ws.UserFavoriteService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;


@GraphQL2Module(type = GraphQL2Module.Type.User, description = "用户收藏夹")
public class UserFavoriteGraphql {

    @Reference
    private UserFavoriteService userFavoriteService;

    @Reference
    private UserFavoriteItemsService userFavoriteItemsService;

    @TypeField(type = UserFavorite.class, field = "canPublic", description = "能公开")
    public boolean canPublic(@Source UserFavorite source) {
        return !source.getPublicity();
    }

    @TypeField(type = UserFavorite.class, field = "canCancelPublic", description = "能取消公开")
    public boolean canCancelPublic(@Source UserFavorite source) {
        return source.getPublicity();
    }

    @TypeField(type = UserFavorite.class, field = "canDelete", description = "能删除")
    public boolean canDelete(@Source UserFavorite source) {
        return !source.getIsDefault();
    }

    @TypeField(type = UserFavorite.class, field = "canEdit", description = "能编辑")
    public boolean canEdit(@Source UserFavorite source) {
        return !source.getIsDefault();
    }

    @TypeField(type = UserFavorite.class, field = "userFavoriteItemsList", description = "收藏内容列表")
    public Pagination<UserFavoriteItems> userFavoriteItemsList(@Source UserFavorite source, @NonNull Page page) {
        return userFavoriteItemsService.queryUserFavoriteItemsByFavoriteId(source.getFavoriteId(), page);
    }

    @TypeField(type = UserFavorite.class, field = "favoriteItemsNumber", description = "收藏数量")
    public long favoriteItemsNumber(@Source UserFavorite source) {
        return userFavoriteItemsService.countByFavoriteId(source.getFavoriteId());
    }

    @QueryField(field = "userFavoriteList", description = "收藏夹列表")
    public Pagination<UserFavorite> queryUserFavoriteList(@NonNull Sorting[] sorts, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return userFavoriteService.queryUserFavoriteList(user.userId(), sorts, page);
    }

    @MutationField(field = "addUserFavorite", description = "创建收藏夹")
    public UserFavorite add(@NonNull AddUserFavoriteDTO add, ConsumerMemberAuthority user) {
        user.assertLogin();
        return userFavoriteService.addUserFavorite(user.userId(), add, user.userName());
    }

    @MutationField(field = "editUserFavorite", description = "修改收藏夹")
    public UserFavorite edit(@NonNull EditUserFavoriteDTO edit, ConsumerMemberAuthority user) {
        user.assertLogin();
        UserFavorite record = userFavoriteService.getUserFavoriteByIdAndUserId(user.userId(), edit.getFavoriteId());
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "收藏夹不存在");
        BeanUtil.copy(edit, record);
        return userFavoriteService.editUserFavorite(record, user.userName());
    }

    @MutationField(field = "deleteUserFavorite", description = "删除收藏夹")
    public boolean del(@NonNull Integer id, ConsumerMemberAuthority user) {
        user.assertLogin();
        String updateUser = user.userName();
        UserFavorite record = userFavoriteService.getUserFavoriteByIdAndUserId(user.userId(), id);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "收藏夹不存在");
        userFavoriteService.deleteUserFavorite(record, updateUser);
        return true;
    }

    @MutationField(field = "setPublicUserFavorite", description = "公开收藏夹")
    public boolean setPublic(@NonNull Integer id, ConsumerMemberAuthority user) {
        user.assertLogin();
        UserFavorite record = userFavoriteService.getUserFavoriteByIdAndUserId(user.userId(), id);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "收藏夹不存在");
        userFavoriteService.setPublic(record, user.userName());
        return true;
    }

    @MutationField(field = "cancelPublicUserFavorite", description = "取消公开收藏夹")
    public boolean cancelPublic(@NonNull Integer id, ConsumerMemberAuthority user) {
        user.assertLogin();
        UserFavorite record = userFavoriteService.getUserFavoriteByIdAndUserId(user.userId(), id);
        Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "收藏夹不存在");
        userFavoriteService.cancelPublic(record, user.userName());
        return true;
    }
}
